package com.redisdemo.config;

import com.redisdemo.job.MessageJob;
import com.redisdemo.job.SendMessageJob;
import jakarta.annotation.PostConstruct;
import lombok.AllArgsConstructor;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * <h3>redisDemo</h3>
 * <p>定时任务配置</p>
 *
 * @author gdw
 * @since 2024-06-25 16:19:57
 */
@Configuration
public class QuartzConfig {
    /**
     *定义消息任务
     * @return
     */
    @Bean
    public JobDetail sampleJobDetail() {
        return JobBuilder.newJob(MessageJob.class)
                .withIdentity("receiveMessage", "messageGroup")
                .storeDurably()
                .build();
    }

    /**
     * 定义执行时间
     * @return
     */
    @Bean
    public Trigger sampleTrigger() {
        SimpleScheduleBuilder scheduleBuilder=SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInSeconds(1)
                .repeatForever();
        return TriggerBuilder.newTrigger()
                .forJob(sampleJobDetail())
                .withIdentity("receiveTrigger", "messageGroup")
                .withSchedule(scheduleBuilder)
                .build();
    }
    @Bean
    public JobDetail sendJobDetail() {
        return JobBuilder.newJob(SendMessageJob.class)
                .withIdentity("sendJob", "sendGroup")
                .storeDurably().build();
    }

    @Bean
    public Trigger sendTrigger() {
        SimpleScheduleBuilder scheduleBuilder=SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInSeconds(5)
                .repeatForever();
        return TriggerBuilder.newTrigger()
                .forJob(sendJobDetail())
                .withIdentity("sendJob", "sendGroup")
                .withSchedule(scheduleBuilder)
                .build();
    }

}
